<?php
/**
 * Created by PhpStorm.
 * Script Name: MiniTemplate.php
 * Create: 2023/2/6 16:34
 * Description:
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace plugin\wechat\app\service;


use EasyOpen\Tencent\Request\Mp\ComponentGetPrivacySetting;
use EasyOpen\Tencent\Request\Mp\ComponentSetPrivacySetting;
use EasyOpen\Tencent\Request\Mp\WxaAddToTemplate;
use EasyOpen\Tencent\Request\Mp\WxaCommit;
use EasyOpen\Tencent\Request\Mp\WxaDeleteTemplate;
use EasyOpen\Tencent\Request\Mp\WxaGetAuditStatus;
use EasyOpen\Tencent\Request\Mp\WxaGetPage;
use EasyOpen\Tencent\Request\Mp\WxaGetQrcode;
use EasyOpen\Tencent\Request\Mp\WxaGetTemplateDraftList;
use EasyOpen\Tencent\Request\Mp\WxaGetTemplateList;
use EasyOpen\Tencent\Request\Mp\WxaGetVersionInfo;
use EasyOpen\Tencent\Request\Mp\WxaRelease;
use EasyOpen\Tencent\Request\Mp\WxaSpeedupAudit;
use EasyOpen\Tencent\Request\Mp\WxaSubmitAudit;
use EasyOpen\Tencent\Request\Mp\WxaUndoCodeAudit;
use plugin\wechat\app\model\MiniTemplate as TemplateM;

class MiniTemplate extends Mini
{
    //0待提交审核 1为， 2为审核成功，3为审核失败，4为已发布, 5:为已撤回 6审核延后
    const READY = 0;
    const VERIFYING = 1;
    const SUCCESS = 2;
    const FAIL = 3;
    const PUBLISHED = 4;
    const CANCEL = 5;
    const DELAY = 6;

    const TYPE_TEMPLATE = 1;
    const TYPE_DRAFT = 2;

    static $model = null;

    static function model(){
        if(is_null(self::$model)){
            self::$model = new TemplateM();
        }
        return self::$model;
    }

    /**
     * 数据库模版列表
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function getDictList(){
        return self::model()->where('type', self::TYPE_TEMPLATE)
            ->order('id', 'desc')
            ->column('user_desc', 'template_id');
    }

    /**
     * 同步草稿箱
     * @return mixed
     * @throws \Exception
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function syncDraft(){
        $request = new WxaGetTemplateDraftList();
        $response = self::tencentOpenCli()->execute($request, OpenPlatform::getComponentAccessToken());
        if(empty($response['code'])){
            return $response['errmsg'];
        }
        return $response['data'];
    }

    /**
     * 同步模板
     * @return mixed
     * @throws \Exception
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function syncTemplate(){
        $request = new WxaGetTemplateList();
        $response = self::tencentOpenCli()->execute($request, OpenPlatform::getComponentAccessToken());
        if(empty($response['code'])){
            return $response['errmsg'];
        }
        return $response['data'];
    }

    /**
     * 草稿设为模板
     * @param array $params
     * @return bool|mixed
     * @throws \Exception
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function draftToTemplate($params = []){
        $request = new WxaAddToTemplate();
        $request->setDraftId($params['template_id']);
        $response = self::tencentOpenCli()->execute($request, OpenPlatform::getComponentAccessToken());
        if(empty($response['code'])){
            return $response['errmsg'];
        }
        return true;
    }

    /**
     * 删除模板
     * @param array $params
     * @return bool|mixed
     * @throws \Exception
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function delete($params = []){
        $request = new WxaDeleteTemplate();
        $request->setTemplateId($params['template_id']);
        $response = self::tencentOpenCli()->execute($request, OpenPlatform::getComponentAccessToken());
        if(empty($response['code'])){
            return $response['errmsg'];
        }
        return true;
    }

    /**
     * 提交代码
     * @param array $params
     * @param null $mini_info
     * @return array|mixed|object
     * @throws \EasyWeChat\Kernel\Exceptions\HttpException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function commit($params = [], $mini_info = null){
        $request = new WxaCommit();
        $request->setTemplateId($params['template_id']);

        /*$ext_json = json_encode([
            'ext' => [
                'appId' => $mini_info['appid'],
                'env' => array_merge(['version' => $params['user_version']], $params['ext_json']),
            ]
        ]); //还要加其他的配置参数，例如地图key，七牛key等*/
        $request->setExtJson($params['ext_json']);
        $request->setUserVersion($params['user_version']);
        $request->setUserDesc($params['user_desc']);
        $response = self::tencentOpenCli()->execute($request, self::getAccessToken($mini_info));
        if(empty($response['code'])) {
            return $response['errmsg'];
        }
        return $response;
    }

    /**
     * 获取体验码
     * @param null $mini_info
     * @return mixed
     * @throws \EasyWeChat\Kernel\Exceptions\HttpException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function qrcodeGet($mini_info = null){
        $request = new WxaGetQrcode();
        $response = self::tencentOpenCli()->execute($request, self::getAccessToken($mini_info), true);
        if(empty($response['code'])) {
            return $response['errmsg'];
        }
        return $response;
    }

    /**
     * 提交审核
     * @param array $params
     * @param null $mini_info
     * @return mixed
     * @throws \EasyWeChat\Kernel\Exceptions\HttpException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function auditSubmit($params = [], $mini_info = null){
        $request = new WxaSubmitAudit();
        $request->setItemList($params['item_list']);
        $request->setPrivateApiNotUse(true);
        !empty($params['version_desc']) && $request->setVersionDesc($params['version_desc']);
        $response = self::tencentOpenCli()->execute($request, self::getAccessToken($mini_info));
        if(empty($response['code'])) {
            return $response['errmsg'];
        }
        return $response['data'];
    }

    /**
     * 获取小程序页面配置
     * @param null $mini_info
     * @return mixed
     * @throws \EasyWeChat\Kernel\Exceptions\HttpException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function pageGet($mini_info = null){
        $request = new WxaGetPage();
        $response = self::tencentOpenCli()->execute($request, self::getAccessToken($mini_info));
        if(empty($response['code'])) {
            return $response['errmsg'];
        }
        return $response['data'];
    }

    /**
     * 隐私设置
     * @param array $params
     * @param null $mini_info
     * @return bool|mixed
     * @throws \EasyWeChat\Kernel\Exceptions\HttpException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function privacySettingSet($params = [], $mini_info = null){
        $request = new ComponentSetPrivacySetting();
        $request->setOwnerSetting([
            'contact_phone' => $params['contact_phone'],
            'notice_method' => $params['notice_method']
        ]);

        $setting_list = [];
        foreach ($params['setting_list'] as $k => $v){
            $v && $setting_list[] = [
                'privacy_key' => $k,
                'privacy_text' => $v
            ];
        }
        $request->setSettingList($setting_list);
        $response = self::tencentOpenCli()->execute($request, self::getAccessToken($mini_info));
        if(empty($response['code'])){
            return $response['errmsg'];
        }
        return true;
    }

    /**
     * 获取隐私设置
     * @param null $mini_info
     * @return mixed
     * @throws \EasyWeChat\Kernel\Exceptions\HttpException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function privacySettingGet($mini_info = null){
        $request = new ComponentGetPrivacySetting();
        $response = self::tencentOpenCli()->execute($request, self::getAccessToken($mini_info));
        if(empty($response['code'])){
            return $response['errmsg'];
        }
        return $response['data'];
    }

    /**
     * 版本审核状态查看
     * @param array $params
     * @param null $mini_info
     * @return bool|mixed
     * @throws \EasyWeChat\Kernel\Exceptions\HttpException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function auditStatusGet($params = [], $mini_info = null){
        $request = new WxaGetAuditStatus();
        $request->setAuditId($params['auditid']);
        $response = self::tencentOpenCli()->execute($request, self::getAccessToken($mini_info));
        if(empty($response['code'])){
            return $response['errmsg'];
        }
        return $response['data'];
    }

    /**
     * 撤销审核
     * @param null $mini_info
     * @return bool|mixed
     * @throws \EasyWeChat\Kernel\Exceptions\HttpException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function auditCancel($mini_info = null){
        $request = new WxaUndoCodeAudit();
        $response = self::tencentOpenCli()->execute($request, self::getAccessToken($mini_info));
        if(empty($response['code'])){
            return $response['errmsg'];
        }
        return true;
    }

    /**
     * 发布
     * @param null $mini_info
     * @return bool|mixed
     * @throws \EasyWeChat\Kernel\Exceptions\HttpException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function release($mini_info = null){
        $request = new WxaRelease();
        $response = self::tencentOpenCli()->execute($request, self::getAccessToken($mini_info));
        if(empty($response['code'])) {
            return $response['errmsg'];
        }
        return true;
    }

    /**
     * 获取线上版本信息
     * @param null $mini_info
     * @return array|mixed
     * @throws \EasyWeChat\Kernel\Exceptions\HttpException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function getVersionInfo($mini_info = null){
        $request = new WxaGetVersionInfo();
        $response = self::tencentOpenCli()->execute($request, self::getAccessToken($mini_info));
        if(empty($response['code'])) {
            return $response['errmsg'];
        }
        return empty($response['data']['release_info']) ? [] : $response['data']['release_info'];
    }

    /**
     * 版本状态
     * @param null $id
     * @return array|mixed
     * @author: fudaoji<fdj@kuryun.cn>
     */
    public static function statusList($id = null){
        $list = [
            self::READY => '待提交审核',
            self::VERIFYING => '审核中',
            self::SUCCESS => '审核通过',
            self::FAIL => '审核失败',
            self::PUBLISHED => '已发布',
            self::CANCEL => '已撤回',
            self::DELAY => '审核延后'
        ];
        return isset($list[$id]) ? $list[$id] : $list;
    }

    /**
     * 审核加速
     * @param array $params
     * @param null $mini_info
     * @return bool|mixed
     * @throws \EasyWeChat\Kernel\Exceptions\HttpException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function auditSpeedUp($params = [], $mini_info = null){
        $request = new WxaSpeedupAudit();
        $request->setAuditId($params['audit_id']);
        $response = self::tencentOpenCli()->execute($request, self::getAccessToken($mini_info));
        if(empty($response['code'])){
            return $response['errmsg'];
        }
        return true;
    }
}